package com.mj.torrentsearch.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.mj.torrentsearch.bean.TorrentDto;
import com.mj.torrentsearch.bean.TorrentList;
import com.mj.torrentsearch.util.SPUtils;
import com.mj.torrentsearch.util.HttpUtils;

import android.content.Context;

/**
 * 种子搜索
 * @author zhaominglei
 * @date 2015-6-14
 * 
 */
public class TorrentsearchService extends BaseService {
	@SuppressWarnings("unused")
	private static final String TAG = TorrentsearchService.class.getSimpleName();
	private static final String TORRENT_INDEX_URL = "http://www.btmgg.com"; //btmgg
	private static final String TORRENT_SEARCH_URL = "http://www.btmgg.com/list/"; //btmgg
	
	public void syncLatestTorrent(Context context) {
		String html = HttpUtils.doGetForBtmgg(TORRENT_INDEX_URL, null);
		if (html != null && !html.equals("")) {
			html = html.replaceAll("<i class=\"new\">新</i>", "");
			Document document = Jsoup.parse(html);
			Elements elements = document.getElementsByClass("info-box");
			if (elements != null && !elements.isEmpty()) {
				SPUtils.clear(context);
				Element element = elements.get(0);
				Elements a_els = element.getElementsByTag("a");
				for (Element a_el : a_els) {
					String a_href = a_el.attr("href");
					String a_text = a_el.text();
					if (a_text.indexOf("更多>>") > -1) continue;
					SPUtils.put(context, a_text, a_href);
				}
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<TorrentDto> getLatestTorrent(Context context) {
		Map<String, String> map = (Map<String, String>) SPUtils.getAll(context);
		if (map == null || map.isEmpty()) {
			syncLatestTorrent(context);
		}
		map = (Map<String, String>) SPUtils.getAll(context);
		List<TorrentDto> torrentDtos = new ArrayList<TorrentDto>();
		for (Map.Entry<String, String> entry : map.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			
			TorrentDto dto = new TorrentDto();
			dto.setName(key);
			dto.setSearchURL(value);
			torrentDtos.add(dto);
		}
		return torrentDtos;
	}
	
	public TorrentList getTorrentList(Context context, String keyword, int page) {
		String searchURL = TORRENT_SEARCH_URL + HttpUtils.encodeURI(keyword) + "/" + page;
		String html = HttpUtils.doGetForBtmgg(searchURL, null);
		if (html != null && !html.equals("")) {
			TorrentList torrentList = new TorrentList();
			List<TorrentDto> torrentDtos = new ArrayList<TorrentDto>();
			Document document = Jsoup.parse(html);
			Elements elements = document.getElementsByClass("item-title");
			if (elements != null && !elements.isEmpty()) {
				for (Element element : elements) {
					Elements a_els = element.getElementsByTag("a");
					if (a_els == null || a_els.isEmpty()) continue;
					Element a_el = a_els.get(0);
					String a_href = a_el.attr("href");
					String a_text = a_el.text();
					TorrentDto dto = new TorrentDto();
					dto.setName(a_text);
					dto.setDetailURL(TORRENT_INDEX_URL + a_href);
					torrentDtos.add(dto);
				}
			}
			torrentList.setRecordList(torrentDtos);
			return torrentList;
		}
		return null;
	}
	
	public TorrentDto getTorrentDetail(Context context, String name, String detailURL) {
		if (detailURL == null || detailURL.equals("")) {
			return null;
		}
		String html = HttpUtils.doGetForBtmgg(detailURL, null);
		if (html != null && !html.equals("")) {
			TorrentDto dto = new TorrentDto();
			if (html.indexOf("错误 - 磁力搜索 - BT芒果干") > -1) {
				dto.setSuccess("false");
			} else {
				dto.setSuccess("true");
				Map<String,String> summaryMap = new LinkedHashMap<String, String>();
				Document document = Jsoup.parse(html);
				Elements tr_els = document.getElementsByTag("tr");
				if (tr_els != null && !tr_els.isEmpty() && tr_els.size() == 2) {
					Element tr_el = tr_els.get(1);
					Elements td_els = tr_el.getElementsByTag("td");
					for (int i = 0; (td_els != null && td_els.size() > 0) && i < td_els.size(); i++) {
						Element td_el = td_els.get(i);
						String content = td_el.text();
						if (i == 0) {
							summaryMap.put("文件类型", content);
						}
						if (i == 1) {
							summaryMap.put("创建时间", content);
						}
						if (i == 2) {
							summaryMap.put("最近下载", content);
						}
						if (i == 3) {
							summaryMap.put("下载热度", content);
						}
						if (i == 4) {
							summaryMap.put("文件大小", content);
						}
						if (i == 5) {
							summaryMap.put("文件数量", content);
						}
					}
				}
				dto.setSummaryMap(summaryMap);
				Elements div_els = document.getElementsByClass("panel-body");
				for (int i = 0; (div_els != null && div_els.size() > 0) && i < div_els.size(); i++) {
					Element div_el = div_els.get(i);
					Elements a_els = div_el.getElementsByTag("a");
					if (a_els == null || a_els.isEmpty()) continue;
					Element a_el = a_els.get(0);
					String a_href = a_el.attr("href");
					if (i == 0) {
						dto.setDownloadURL_magnet(a_href);
					}
					if (i == 1) {
						dto.setDownloadURL_thunder(a_href);
					}
				}
			}
			return dto;
		}
		return null;
	}
}
